package com.pt.manage.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.Security;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.pt.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.pt.common.annotation.Log;
import com.pt.common.core.controller.BaseController;
import com.pt.common.core.domain.AjaxResult;
import com.pt.common.enums.BusinessType;
import com.pt.manage.domain.MysHomeWork;
import com.pt.manage.service.ImyshomeworkService;
import com.pt.common.utils.poi.ExcelUtil;
import com.pt.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import static cn.hutool.core.io.FileUtil.writeFromStream;
/**
 * 学生作业Controller
 * 
 * @author ruoyi
 * @date 2025-03-02
 */
@RestController
@RequestMapping("/manage/mysHomework")
public class myshomeworkController extends BaseController
{
    @Value("${ruoyi.filePath}")
    private String sysFilePath;
    @Autowired
    private ImyshomeworkService myshomeworkService;

    /**
     * 查询学生作业列表
     */

    @GetMapping("/list")
    public TableDataInfo list(MysHomeWork myshomework,@RequestParam("courseID") String courseID )
    {

        startPage();
        Map<String, Object> params = new HashMap<>();
        myshomework.setStudentID(SecurityUtils.getUserId());
        params.put("courseID", courseID); //
        params.put("myshomework", myshomework); //
        List<MysHomeWork> list = myshomeworkService.selectmyshomeworkList(params);
        return getDataTable(list);
    }

    /**
     * 新增学生作业文件
     */
    @PostMapping("/addHomeworkFile")
        public AjaxResult addHomeworkFile(@RequestParam("file") MultipartFile wordFile,
                                          @RequestParam("codefile") MultipartFile codeFile,
                                          @RequestParam("homeworkID") Long homeworkID)
        {


            //获取文件名称
            String codeNewUrl = "/"+codeFile.getOriginalFilename();
            String codePath ;
            String wordPath ;
            try {
                wordPath = "/wordFile"+"/"+homeworkID+"/"+SecurityUtils.getUserId()+wordFile.getOriginalFilename();
                //复制文件
                writeFromStream(wordFile.getInputStream(),sysFilePath+wordPath);
            } catch (IOException e) {
                return AjaxResult.error("代码文件上传失败：" + e.getMessage());
            }
            //获取文件名称
            String fileNewUrl = "/"+wordFile.getOriginalFilename();
            try {
                codePath = "/codeFile"+"/"+homeworkID+"/"+SecurityUtils.getUserId()+codeFile.getOriginalFilename();
                //复制文件
                writeFromStream(codeFile.getInputStream(),sysFilePath+codePath);
            } catch (IOException e) {
                return AjaxResult.error("word文件上传失败：" + e.getMessage());
            }
            MysHomeWork myshomework = new MysHomeWork();
            myshomework.setHomeworkID(homeworkID);
            myshomework.setStudentID(SecurityUtils.getUserId());
            myshomework.setCodeUrl(codePath);
            myshomework.setWordUrl(wordPath);
            myshomework.setStartTime(new Date());
            myshomework.setStatus(MysHomeWork.WAIT_STATUS);
            myshomeworkService.updatemyshomework(myshomework);
            return toAjax(1);

        }
//    /**
//     * 导出学生作业列表
//     */
//    @PreAuthorize("@ss.hasPermi('manage:mysHomework:export')")
//    @Log(title = "学生作业", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, MysHomeWork myshomework)
//    {
//        List<MysHomeWork> list = myshomeworkService.selectmyshomeworkList(myshomework);
//        ExcelUtil<MysHomeWork> util = new ExcelUtil<MysHomeWork>(MysHomeWork.class);
//        util.exportExcel(response, list, "学生作业数据");
//    }

    /**
     * 获取学生作业详细信息
     */

    @GetMapping(value = "/{studentID}")
    public AjaxResult getInfo(@PathVariable("studentID") Long studentID)
    {
        return success(myshomeworkService.selectmyshomeworkByStudentID(studentID));
    }



    /**
     * 修改学生作业
     */
    @Log(title = "学生作业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MysHomeWork myshomework)
    {
        return toAjax(myshomeworkService.updatemyshomework(myshomework));
    }

    /**
     * 删除学生作业
     */

    @Log(title = "学生作业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIDs}")
    public AjaxResult remove(@PathVariable Long[] studentIDs)
    {
        return toAjax(myshomeworkService.deletemyshomeworkByStudentIDs(studentIDs));
    }

}
